const app = getApp()
const $api = require('../../../utils/api.js').API;
Page({
  data:{
    scrollHeight: '100vh',
    uid:wx.getStorageSync('uid'),
    list:[],
    setInter:'',
    inputValue:'',
    scrollHeight: '100vh',
    toView:'',
  },
  onLoad: function(){
    if(wx.getStorageSync('list')){
      var str = wx.getStorageSync('list').length - 1;
      this.setData({
        list:wx.getStorageSync('list'),
        toView:'A'+ str
      })
    }
    this.auto()
    this.getInfo()
  },
  getInfo:function(){
    var that = this
    var data = {'uid':wx.getStorageSync('uid')}
    $api.get('member/status',data).then(res => {
      //请求成功
      if(res.data.code == 1){
        that.setData({
          avatar:res.data.data.avatar
        })
        }
    })
  },
  messageAdd:function(){
    this.send()
  },
  bindKeyInput:function(e){
    this.setData({
      inputValue: e.detail.value
    })
  },
  onShareAppMessage: function(){
    return {
        title: "天意美业直通车",
        path: "/pages/index/index?puid="+wx.getStorageSync('uid')
    };
  },
  send:function(){
    if(!this.data.inputValue){
      return;
    }
    var that = this
    var arr = {'type':1,'content':this.data.inputValue}
    var data = {'name':this.data.inputValue}
    var list = this.data.list
    $api.post('problem/info',data).then(res =>{
      list.push(arr)
      if(res.data.code == 1 && res.data.data){
        var arr1 = {'type':2,'content':res.data.data.value}
      }else{
        var arr1 = {'type':2,'content':'机器人小美为您服务,如有更多问题请转人工客服,客服电话:18322223377'}
      }
      list.push(arr1)
      var str = list.length - 1;
      this.setData({
        'inputValue':'',
        list:list,
        toView:'A'+ str
      })
      wx.setStorageSync('list',list)
    })
  },
  toSearch:function(e){
    this.send()
  },
  remove:function(){
    wx.removeStorageSync('list')
    this.setData({
      list:[]
    })
  },
  auto:function(){
    var that = this
    var data  = {}
    $api.get('problem/list',data).then(res => {
      //请求成功
      if(res.data.code == 1){
        var arr = res.data.data
        var arr1 = {'type':2, 'content':arr,'role':1}
        var list = this.data.list
        list.push(arr1)
        var str = list.length - 1;
        that.setData({
          toView:'A'+ str,
          list:list
        })
      }
    })
  },
  answer:function(e){
    var value = e.currentTarget.dataset.name
    var arr1 = {'type':2,'content':value}
    var list = this.data.list
    list.push(arr1)
    var str = list.length - 1;
    this.setData({
      list:list,
      toView:'A'+ str
    })
  }
})